#coding: utf-8
class Shared::Province < ActiveRecord::Base
  belongs_to :obj_department, :class_name => 'Shared::Department', :foreign_key => 'department'
  has_many :districts, :class_name => 'Shared::District', :foreign_key => 'province'
  has_many :complaints, :class_name => 'RequestAndComplaint::Complaint', :foreign_key => 'province'
  has_many :cultural_institutions, :class_name => 'CulturalHeritage::CulturalInstitution', :foreign_key => 'province'
  default_scope where("deleted = 0")

  def self.count_department_dependencies(id)
      return find(:all,:conditions=>["shared_provinces.department = ? and (shared_provinces.deleted = ?) ",id,0],:select=>"count(*) as 'provinces'")[0].provinces.to_i
  end

  def erasable
    compla = RequestAndComplaint::Complaint.count_district_dependencies(id)
    cultu_insti = CulturalHeritage::CulturalInstitution.count_district_dependencies(id)
    dist = Shared::District.count_province_dependencies(id)
    inci = CulturalHeritage::Incidence.count_district_dependencies(id)
    reg_req = RequestAndComplaint::RegistrationRequest.count_district_dependencies(id)
    if compla >0
      return "Imposible borrar. Existen dependencias en denuncia"    
    end
    if cultu_insti >0
      return "Imposible borrar. Existen dependencias en institucion cultural"    
    end
    if inci >0
      return "Imposible borrar. Existen dependencias en incidencia"  
    end
    if reg_req >0
      return "Imposible borrar. Existen dependencias en registro de solicitud"   
    end
    if dist >0
      return "Imposible borrar. Existen dependencias en distrito"    
    end
    return nil
  end
end
